package it.univaq.mancoosi.injectors.systemconfiguration.managers;

import it.univaq.mancoosi.injectors.systemconfiguration.managers.ubuntu.UbuntuEnvironmentManager;
import it.univaq.mancoosi.mancoosimm.MancoosiFactory;
import it.univaq.mancoosi.mancoosimm.SGMLDocument;
import it.univaq.mancoosi.mancoosimm.SkeeperCatalog;
import it.univaq.mancoosi.mancoosimm.SkeeperDocument;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

public abstract class SkeeperCatalogManager {

	protected static SkeeperCatalogManager INSTANCE;
	protected MancoosiFactory factory; 
	protected SkeeperCatalog skeeperCatalog;
	protected HashMap<String, SkeeperDocument> skeeperMap;

	protected SkeeperCatalogManager(){
		factory = MancoosiFactory.eINSTANCE;
		skeeperCatalog = factory.createSkeeperCatalog(); 
		this.skeeperMap = new HashMap<String, SkeeperDocument>();
	}

	public static SkeeperCatalogManager getInstance() {
		return INSTANCE;
	}

	public abstract void createSkeeperCatalogFromSystem();
	
	public abstract Boolean exists(String skeeperDocumentName);

	public abstract void synchronize() throws IOException;

	public SkeeperCatalog getSkeeperCatalog() {
		return skeeperCatalog;
	}

	protected void syncMap () {
		Iterator<SkeeperDocument> iterator  = null;
		if(UbuntuEnvironmentManager.getInstance().getEnvironment().getSkeeperCatalog() != null){
			iterator = UbuntuEnvironmentManager.getInstance().getEnvironment().getSkeeperCatalog().getDocuments().iterator();
		}
		try {
			if(iterator != null){
				while (iterator.hasNext()) {
					SkeeperDocument curr = iterator.next();
					this.skeeperMap.put(curr.getName(), curr);
				}
			}
		}
		catch (NullPointerException e) {
			e.printStackTrace();
		}
	}
}
